package yz.com.javautil.test;

/**
 * @author liuxk
 * @since 2021/10/14
 * 坐标系经纬度转换
 */
public class Earth2Mars {
    private double a = 6378245.0;
    private double ee = 0.00669342162296594323;
    private double xPi = Math.PI * 3000.0 / 180.0;
    private double pi = 3.1415926535897932384626;
    /**
     * 单例模式配置
     */

    private static Earth2Mars instance = new Earth2Mars();

    public static Earth2Mars getInstance() {
        return instance;
    }

    /**
     * 判断经纬度是否在中国国内
     */
    private boolean transformSinoOutChina(double lon, double lat) {
        if (lon < 72.004 || lon > 137.8347) {
            return true;
        }
        if (lat < 0.8293 || lat > 55.8271) {
            return true;
        }
        return false;
    }

    /**
     * 纬度计算wgs84到火星坐标系转换
     */
    private double transformEarthToMarsLat(double x, double y) {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * Math.PI) + 40.0 * Math.sin(y / 3.0 * Math.PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * Math.PI) + 320 * Math.sin(y * Math.PI / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    /**
     * 经度计算wgs84到火星坐标系转换
     */
    private double transformEarthToMarsLng(double x, double y) {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * Math.PI) + 40.0 * Math.sin(x / 3.0 * Math.PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * Math.PI) + 300.0 * Math.sin(x / 30.0 * Math.PI)) * 2.0 / 3.0;
        return ret;
    }

    /**
     * 计算wgs84到火星坐标系转换
     */
    public double[] transformEarthToMars(double lng, double lat) {
        double tarLat, tarLng;
        if (transformSinoOutChina(lng, lat)) {
            tarLat = lat;
            tarLng = lng;
            return new double[]{tarLng, tarLat};
        }
        double dLat = transformEarthToMarsLat(lng - 105.0, lat - 35.0);
        double dLon = transformEarthToMarsLng(lng - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * Math.PI;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * Math.PI);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * Math.PI);
        tarLat = lat + dLat;
        tarLng = lng + dLon;
        return new double[]{tarLng, tarLat};
    }



    /**
     * 火星坐标系(GCJ)转换到wgs84
     */
    public double[] transformMarsToEarth(double lng,double lat){
        if (outOfChina(lng, lat)) {
            return new double[]{lng, lat};
        } else {
            double dlat = transformMarsToEarthLat(lng - 105.0, lat - 35.0);
            double dlng = transformMarsToEarthLng(lng - 105.0, lat - 35.0);
            double radlat = lat / 180.0 * pi;
            double magic = Math.sin(radlat);
            magic = 1 - ee * magic * magic;
            double sqrtmagic = Math.sqrt(magic);
            dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi);
            dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi);
            double mglat = lat + dlat;
            double mglng = lng + dlng;
            return new double[]{lng * 2 - mglng, lat * 2 - mglat};
        }
    }

    /**
     *判断经纬度是否在中国国内
     * @param lng
     * @param lat
     * @return
     */
    public boolean outOfChina(double lng, double lat) {
        // 纬度3.86~53.55,经度73.66~135.05
        return !(lng > 73.66 && lng < 135.05 && lat > 3.86 && lat < 53.55);
    }

    /**
     * 纬度计算火星坐标系到wgs84转换
     */
    private double transformMarsToEarthLat(double lng,double lat) {
        double ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
        ret += (20.0 * Math.sin(6.0 * lng * pi) + 20.0 * Math.sin(2.0 * lng * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(lat * pi) + 40.0 * Math.sin(lat / 3.0 * pi)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(lat / 12.0 * pi) + 320 * Math.sin(lat * pi / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    /**
     * 经度计算火星坐标系到wgs84转换
     */
    private double transformMarsToEarthLng(double lng,double lat) {
        double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
        ret += (20.0 * Math.sin(6.0 * lng * pi) + 20.0 * Math.sin(2.0 * lng * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(lng * pi) + 40.0 * Math.sin(lng / 3.0 * pi)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(lng / 12.0 * pi) + 300.0 * Math.sin(lng / 30.0 * pi)) * 2.0 / 3.0;
        return ret;
    }

    /**
     * 把经纬度加密成百度坐标系
     */
    public double[] bdEncrypt(double ggLon, double ggLat) {
        double x = ggLon, y = ggLat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * xPi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * xPi);
        double bdLon = z * Math.cos(theta) + 0.0065;
        double bdLat = z * Math.sin(theta) + 0.006;
        return new double[]{bdLon, bdLat};
    }

    /**
     * 把百度经纬度解密成坐标系
     */
    public double[] bdDecrypt(double bdLon, double bdLat) {
        double x = bdLon - 0.0065, y = bdLat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * xPi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * xPi);
        double ggLon = z * Math.cos(theta);
        double ggLat = z * Math.sin(theta);
        return new double[]{ggLon, ggLat};
    }
    /**
     * 把火星坐标(GCJ)系转换成百度坐标系(BD)
     */
    public double[] transformMarsToBaidu(double lng, double lat){
        double z = Math.sqrt(lng * lng+ lat * lat)+ 0.00002 * Math.sin(lat * xPi);
        double theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * xPi);
        double bdLng = z * Math.cos(theta) + 0.0065;
        double bdLat = z * Math.sin(theta) + 0.006;
        return new double[]{bdLng, bdLat};
    }

    /**
     * 把百度坐标系(BD)转换成火星坐标系(GCJ)
     */
    public double[] transformBaiduToMars(double bdLon,double bdLat){
        double x = bdLon - 0.0065;
        double y = bdLat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * xPi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * xPi);
        double ggLng = z * Math.cos(theta);
        double ggLat = z * Math.sin(theta);
        return new double[]{ggLng, ggLat};
    }

    /**
     * 计算wgs84到BD坐标系转换
     */
    public double[] transformEarthToBaidu(double lng,double lat){
        double[] gcj = transformEarthToMars(lng,lat);
        return transformMarsToBaidu(gcj[0], gcj[1]);
    }

    /**
     * 计算BD到wgs84坐标系转换
     */
    public double[] transformBaiduToEarth(double bdLon,double bdLat){
        double[] gcj = transformBaiduToMars(bdLon,bdLat);
        return transformMarsToEarth(gcj[0],gcj[1]);
    }
}
